<template>
  <n-card title="当日统计">
<!--    {{data}}-->
    <template #header-extra>
      <n-space>
        <MyDatePicker4Date v-model:time="time"></MyDatePicker4Date>
        <n-switch type="primary" @click="type=type==1?2:1">
          <template #checked>
            扇形
          </template>
          <template #unchecked>
            环形
          </template>
        </n-switch>
      </n-space>
    </template>
    <SemiCircleTimePie :type="type" :data="data" :key="type"></SemiCircleTimePie>
  </n-card>
</template>

<script lang="ts" setup>

import SemiCircleTimePie from "@/views/taskoperation/tasklog/common/SemiCircleTimePie.vue";
import {computed, onMounted, ref, watch} from "vue";
import {post, Response} from "@/api/http";
import {addTaskLog, getDayTakeTime4Stat} from "@/api/url";
import {useMessage} from "naive-ui";
import useUserStore from "@/store/modules/user";
import MyDatePicker from "@/views/task/time/MyDatePicker.vue";
import MyDatePicker4Date from "@/views/task/time/MyDatePicker4Date.vue";

const message = useMessage()
const userStore = useUserStore()
const time = ref(Date.now())
const data = computed(()=>{
  if (type.value === 1){
    return oldData.value;
  }
  if (type.value === 2){
    const list = new Map();
    oldData.value.map((value1:any) => {
      let time = list.get(value1.name)
      if(!time) time = 0;
      list.set(value1.name,time+value1.value)
    });
    const results = [];
    for (const listKey of list) {
      // console.log(listKey)
      results.push({ name: listKey[0], value: listKey[1] })
    }
    return results;
  }
  return []
})
const oldData =  ref([])
const type = ref(1)
const getData =  async ()=>{
  try {
    const data: Response = await post({
      url: getDayTakeTime4Stat,
      data: {time:time.value},
      token:userStore.getToken()
    });
    console.log(data.data.data)
    // message.success(data.msg)
    return data.data.data;
  }catch(e) {
    message.error((e as Error).message)
    return [];
  }finally {
  }
  return [];
}
onMounted(async ()=>{
  oldData.value = await getData()
})
watch(time,async ()=>{
  oldData.value = await getData()
})
</script>
